import { Message, Middleware } from 'mirai-js'
import { Bot as BotType } from 'mirai-js/dist/node/index'
import jsonfile from 'jsonfile'
import path from 'path'
const middleware = new Middleware()
import fs from 'fs'
import { textCommandFilter } from '../../middleware/textCommandFilter'
import { hasMcResMessage, mcMessage, mcpingServer, McSever } from '../../utils'
export default (bot: BotType) => {
  // 监听群消息事件
  bot.on(
    'GroupMessage',
    new textCommandFilter('ping').done(async (data) => {
      try {
        const [host, port = 25565] = data.text.split(':')
        const res = (await mcpingServer(host, Number(port)).catch((err) => {
          console.log(err)
          bot.sendMessage({
            group: data.sender.group.id,
            message: new Message().addText(`服务器未开启或者ip地址填写错误`),
          })
          return false
        })) as McSever
        if (!res) return

        const hasRes = hasMcResMessage(res)
        bot.sendMessage({
          group: data.sender.group.id,
          message: new mcMessage(hasRes),
        })
      } catch (err) {
        console.log(err)
        console.log(data.text)
      }
    }),
  )
}
